package com.flyme.module.base.provider.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.JsonUtils;
import com.flyme.common.core.utils.STSOSSTokenUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysDictDataRemoteService;
import com.flyme.module.base.client.entity.SysConfig;
import com.flyme.module.base.client.entity.SysDict;
import com.flyme.module.base.client.entity.SysDictData;
import com.flyme.module.base.provider.service.SysConfigService;
import com.flyme.module.base.provider.service.SysDictDataService;
import com.flyme.module.base.provider.service.SysDictService;
import com.flyme.module.file.provider.utils.AliStsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 */
@Slf4j
@RestController
@Api(description="字典数据管理接口")
@RequestMapping("/dictdata")
public class SysDictDataController extends BaseController<SysDictDataService,SysDictData> implements SysDictDataRemoteService{

    @Autowired
    private SysDictService dictService;
    @Autowired
    private SysConfigService configService;

    @PostMapping(value="/list")
    @ApiOperation(value="多条件分页获取用户列表")
    @Override
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysDictData dictData){
        CriteriaQuery cq=new CriteriaQuery<EntityMap>(pagerInfo);
        cq.eq("dictId",dictData.getDictId());
        cq.eq("dicStatus",dictData.getDicStatus());
        cq.select("id","dicDataTitle","dicDataValue","sortOrder","createTime","dicStatus","remark");
        PqGrid pqGrid=bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @GetMapping(value="/getByType/{type}")
    @ApiOperation(value="通过类型获取")
    @Override
    public ResultBody<Object> getByType(@PathVariable String type){

        SysDict dict=dictService.findByType(type);
        if(dict==null){
            return ResultBody.failed("字典类型Type不存在");
        }
        List<SysDictData> list=bizService.findByDictId(dict.getId());
        return ResultBody.success().setData(list);
    }

    @PostMapping(value="/add")
    @ApiOperation(value="添加")
    @Override
    public ResultBody<Object> add(@ModelAttribute SysDictData dictData){

        SysDict dict=dictService.getById(dictData.getDictId());
        if(dict==null){
            return ResultBody.failed("字典类型id不存在");
        }
        bizService.save(dictData);
        dictService.initDictRedis();
        return ResultBody.ok("添加成功");
    }

    @PostMapping(value="/edit")
    @ApiOperation(value="编辑")
    @Override
    public ResultBody<Object> edit(@ModelAttribute SysDictData dictData){
        bizService.saveOrUpdate(dictData);
        dictService.initDictRedis();
        return ResultBody.ok("编辑成功");
    }

    @DeleteMapping(value="/delByIds/{ids}")
    @ApiOperation(value="批量通过id删除")
    @Override
    public ResultBody<Object> delByIds(@PathVariable Long[] ids){
        bizService.removeByIds(CollectionUtil.toList(ids));
        dictService.initDictRedis();
        return ResultBody.ok("批量通过id删除数据成功");
    }

    @GetMapping(value="/getConfigByType/{type}")
    @ApiOperation(value="通过类型获取")
    @Override
    public ResultBody<Object> getConfigByType(@PathVariable String type){
        SysConfig config = configService.getByName(type);
        if(config==null){
            return ResultBody.failed("该类型不存在");
        }
        return ResultBody.success().setData(config);
    }

    @PostMapping(value="/saveEditIosRait")
    @ApiOperation(value="编辑")
    @Override
    public ResultBody<Object> saveEditIosRait(@ModelAttribute SysConfig config){

        configService.saveOrUpdate(config);
        return ResultBody.ok("编辑成功");
    }

    @PostMapping(value="/saveEditShareRatio")
    @ApiOperation(value="编辑")
    @Override
    public ResultBody<Object> saveEditShareRatio(@RequestParam String shareRatio){
        List<SysConfig> list = JsonUtils.toList(shareRatio, SysConfig.class);
        configService.updateBatchById(list);
        return ResultBody.ok("修改成功");
    }

    @PostMapping(value="/saveEditUpOrDownRatio")
    @ApiOperation(value="编辑")
    @Override
    public ResultBody<Object> saveEditUpOrDownRatio(@RequestParam String upOrDown){
        List<SysConfig> list = JsonUtils.toList(upOrDown, SysConfig.class);
        configService.updateBatchById(list);
        return ResultBody.ok("修改成功");
    }


    @PostMapping(value="/getOssSeting")
    @ApiOperation(value="获取阿里云设置")
    @Override
    public ResultBody getOssSeting(){
        EntityMap map2 = new EntityMap();
        try {
            AssumeRoleResponse response2 = STSOSSTokenUtils.getOssAccessKeySecret();
            map2.put("AccessKeyId", response2.getCredentials().getAccessKeyId());
            map2.put("AccessKeySecret", response2.getCredentials().getAccessKeySecret());
            map2.put("SecurityToken", response2.getCredentials().getSecurityToken());
            map2.put("Expiration", response2.getCredentials().getExpiration());
        } catch (ClientException e) {
            log.error("获取阿里点播信息出错Failed to get a token.");
            log.error("获取阿里点播信息出错Error code: " + e.getErrCode());
            log.error("获取阿里点播信息出错Error message: " + e.getErrMsg());
        }
        return ResultBody.success(map2);
    }


}
